# -*- encoding: utf-8 -*-
# 
# Lúcuma
# Copyright © 2008 Diego Escalante Urrelo  <diegoe@gnome.org>
# 
# Licensed under the GNU General Public License Version 2
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor
# Boston, MA  02110-1301, USA.
# 

from django.db import models
import datetime

dict_imservice = (
    (0, 'AOL'), 
    (1, 'MSN'), 
    (2, 'ICQ'), 
    (3, 'Yahoo'), 
    (4, 'Jabber'), 
    (5, 'Skype'), 
    (6, 'gTalk'), 
)

class MetaPersona(models.Model):
    """
    Aditional information about a Persona (read: useful info)
    """
    name = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)
    title = models.CharField(max_length=100, blank=True, null=True)
    
    # FIXME: should be EmailField in django svn where we can top the max_length
    email = models.CharField(max_length=200)
    
    photo = models.ImageField(upload_to='photos/', blank=True, null=True)
    
    officephone = models.CharField(max_length=50, blank=True, null=True)
    officeext = models.CharField(max_length=10, blank=True, null=True)
    mobilephone = models.CharField(max_length=50, blank=True, null=True)
    homephone = models.CharField(max_length=50, blank=True, null=True)
    
    fax = models.CharField(max_length=50, blank=True, null=True)
    
    im = models.CharField(max_length=200, blank=True, null=True)
    imservice = models.IntegerField(choices=dict_imservice, 
                                    blank=True, null=True)
    def __unicode__(self):
        return '%s %s' % (self.name, self.lastname)

class Persona(models.Model):
    """
    The base class for a Persona
    """
    user = models.CharField(max_length=20, unique=True)
    passwd = models.TextField()

    # metadata store
    meta = models.ForeignKey(MetaPersona, blank=True, null=True)
    
    def __unicode__(self):
        return self.user
    
    def fullname(self):
        """
        Returns the fullname of this Persona object.
        """
        if self.meta and self.meta.name:
            return self.meta
        else:
            return self.user

    def tasks_overdue(self):
        """
        Returns all Tasks assigned to this Persona that are already over their
        due date.
        """
        from lucuma.tasks.models import Task
        today = datetime.date.today()
    
        return Task.objects.filter(
            done__exact=False, 
            responsible__exact=self, 
            due__lt=today)

    def tasks_future(self):
        """
        Returns all Tasks assigned to this Persona that are to be done after 
        today.
        """
        from lucuma.tasks.models import Task
        today = datetime.date.today()
    
        return Task.objects.filter(
            done__exact=False, 
            responsible__exact=self, 
            due__gt=today)

    def tasks_today(self):
        """
        Returns all Tasks assigned to this Persona that are to be done exactly
        today.
        """
        from lucuma.tasks.models import Task
        today = datetime.date.today()

        return Task.objects.filter(
            done__exact=False, 
            responsible__exact=self, 
            due__exact=today)

    def tasks_this_week(self):
        """
        Return all the Tasks assigned to this Persona to be done between
        tomorrow and next week (today + 7 days)
        """
        from lucuma.tasks.models import Task
        today = datetime.date.today()
        tomorrow = today + datetime.timedelta(days=1)
        nextweek = today + datetime.timedelta(weeks=1)

        return Task.objects.filter(
            done__exact=False, 
            responsible__exact=self, 
            due__lte=nextweek, 
            due__gte=tomorrow)

    def tasks_assigned(self):
        """
        Return all Tasks assigned to this Persona.
        """
        from lucuma.tasks.models import Task

        return Task.objects.filter(
            done__exact=False,
            responsible__exact=self).order_by('due')

    def notes_involved(self):
        """
        Return all Notes authored by this Persona.
        """
        from lucuma.notes.models import Note
        return Note.objects.filter(
            author__exact=self)
